import {
	base_url
} from '@/config'
import store from '@/store'
import {reLaunchPage} from '@/js/navigatePage'
import {contentUrl} from '@/js/constant'
export const apiResquest = (url,param,method) => { //prams 为我们需要调用的接口API的参数 下面会贴具体代码
 
	// 判断请求类型
	let headerData = {
		'content-type': 'application/json',
		'accessToken': store.getters['login/token']
	}
 
	let dataObj = param
   
	//  Object.assign({}, setting, theme, network)

	// dataObj = {
	// 	'data': param,
	// 	'token': uni.getStorageSync('token')
	// }
	
	return new Promise((resolve, reject) => {
		if(base_url.indexOf('127.0.0.1') != -1){
			url = url.substring(3)
		}
		 url = base_url + url; //请求的网络地址和局地的api地址组合
		 console.log(url)
		uni.showLoading({
			title: '加载中',
			mask: true
		})
		return uni.request({
			url: url,
			data: dataObj,
			method: method || 'POST',
			header: headerData,
			success: (res) => {
				debugger
				uni.hideLoading()
				if(res.data.code == 504){
					store.dispatch('login/setToken','')
					store.dispatch('login/getUserInfo',null)
					store.dispatch('login/getCheckedAdmin',0)
					reLaunchPage(contentUrl)
					return;
				}else if (res.data.code !== 200) {
					uni.showToast({
						title: '获取数据失败:' + res.data.msg,
						duration: 1000,
						icon: "none"
					})
					return;
				}
				resolve(res.data);
			},
			fail: (err) => {
				reject(err);
				console.log('fail',err)
				uni.hideLoading()
			},
			complete: () => {
				console.log('请求完成')
				uni.hideLoading()
			}
		});
	})
}